package com.situ.water_read.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.situ.util.ResponseData;
import com.situ.water_read.pojo.RdVolume;
import com.situ.water_read.service.RdVolumeService;
import com.situ.water_user.pojo.UsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Angel
 * @since 2020-12-01
 */
@RestController
@RequestMapping("/rd-volume")
public class RdVolumeController {
    @Autowired
    RdVolumeService service;

    /**
     * 添加表册
     * @param volume
     * @return
     */
    @PostMapping("/add")
    public ResponseData add(RdVolume volume){
        volume.setDisabled(false);
        int i = service.addvolume(volume);
        System.out.println(i);
        if(i>0) {
            return ResponseData.ok();
        }
        return ResponseData.Failure("添加错误");
    }

    /**
     * 修改表册
     */
    @PutMapping("/updateVolume")
    public ResponseData updateVolume(RdVolume volume){
        boolean bool = service.updateById(volume);
        System.out.println(bool);
        if(bool) {
            return ResponseData.ok();
        }
        return ResponseData.Failure("修改错误");
    }
    /**
     * 禁用表册
     */
    @DeleteMapping("/deleteVolume")
    public void deleteVolume(Integer id){
        System.out.println("表册ID"+id);
        List<UsUser> list=service.selectUserbyId(id);
        for (UsUser user:list){
            user.setVolumeID(null);
            user.setVolumeOrderIndex(null);
            service.updateUserByVolume(user);
        }
        service.deletevolume(id);
    }
    /**
     * 查询表册
     */
    @GetMapping("/queryall")
    public ResponseData queryall(){
        List<RdVolume> list=service.queryall();
        return ResponseData.ok().putDataValue("list",list);
    }

    /**
     * 修改用户的表册id,并在抄表数据中添加用户数据
     */
    @GetMapping("/updatevolumeId")
    public ResponseData updatevolumeId(String[] checkval,Integer volumeID){


        int i = service.updatevolumeId(checkval, volumeID);
        System.out.println(i);
        if(i>0){
            return ResponseData.ok();
        }

        return ResponseData.Failure("失败了呀");
    }
    /**
     * 查询用户是否有表册Id
     */
    @GetMapping("/querybyId")
    public void queryById(Integer volumeID){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("volumeID",volumeID);
        List<UsUser> list=service.querybyId(wrapper);
        list.forEach(x-> System.out.println(x.getVolumeID()));
    }

    /**
     * 修改用户在表册中的序号
     */
    @PostMapping("/updatevolumeOrderIndex")
    public int  updatevolumeOrderIndex(UsUser user){
        int i=service.updateOrderId(user);
        return i;
    }

    /**
     * 获取抄表年月表的总条数
     * @return
     */
    @GetMapping("/queryCount")
    public int queryCount(){
        int max=service.queryCount();
        return max;
    }

}

